<?php

namespace Api\InterfaceApi\v1;

use App\Music\Faith\MusicApiFactory;
use Mine\Annotation\Api\MApi;
use Mine\MineRequest;
use Mine\MineResponse;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

class FaithApi
{
    /**
     * @var MineRequest|mixed
     */
    private mixed $request;
    /**
     * @var MineResponse|mixed
     */
    private mixed $response;

    protected \App\Music\Faith\MusicApi $musicApi;

    /**
     * FaithApi constructor.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function __construct()
    {
        $this->response = container()->get(MineResponse::class);
        $this->request = container()->get(MineRequest::class);

        $music_source = $this->request->input('music_source', 'tencent');
        $musicApi = new MusicApiFactory();
        $this->musicApi = $musicApi->createMusicApi($music_source);
    }

    /**
     * 搜索功能.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'search', name: '搜索功能', description: '', appId: '3aed503b9a', groupId: 1)]
    public function search(): ResponseInterface
    {
        $keyword = $this->request->input('keyword', '');
        $type = $this->request->input('type', 0);
        $page = $this->request->input('page', 1);
        $pageSize = $this->request->input('pageSize', 20);
        $res = $this->musicApi->search($keyword, $type, $page, $pageSize);
        return $this->response->success('请求成功', $res);
    }

    /**
     * 专辑详情.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'album', name: '专辑', description: '', appId: '3aed503b9a', groupId: 1)]
    public function album(): ResponseInterface
    {
        $id = $this->request->input('id', '');
        if (empty($id)) {
            return $this->response->error('请填写ID');
        }
        $res = $this->musicApi->album($id);
        return $this->response->success('请求成功', $res);
    }

    /**
     * 歌词详情.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'lyric', name: '专辑', description: '', appId: '3aed503b9a', groupId: 1)]
    public function lyric(): ResponseInterface
    {
        $id = $this->request->input('id', '');
        $type = $this->request->input('type', '1');
        if (empty($id)) {
            return $this->response->error('请填写ID');
        }
        $res = $this->musicApi->lyric($id, $type);
        return $this->response->success('请求成功', ['lyric' => $res]);
    }

    /**
     * 歌单详情.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'playList', name: '歌单详情', description: '', appId: '3aed503b9a', groupId: 1)]
    public function playList(): ResponseInterface
    {
        $id = $this->request->input('id', '');
        if (empty($id)) {
            return $this->response->error('请填写ID');
        }
        $res = $this->musicApi->playList($id);
        return $this->response->success('请求成功', $res);
    }

    /**
     * 歌曲详情.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'song', name: '歌曲详情', description: '', appId: '3aed503b9a', groupId: 1)]
    public function song(): ResponseInterface
    {
        $id = $this->request->input('id', '');
        if (empty($id)) {
            return $this->response->error('请填写ID');
        }
        $res = $this->musicApi->song($id);
        return $this->response->success('请求成功', $res);
    }

    /**
     * 歌曲播放地址.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'url', name: '歌曲播放地址', description: '', appId: '3aed503b9a', groupId: 1)]
    public function url(): ResponseInterface
    {
        $id = $this->request->input('id', '');
        if (empty($id)) {
            return $this->response->error('请填写ID');
        }
        $res = $this->musicApi->url($id);
        return $this->response->success('请求成功', ['url'=>$res]);
    }

    /**
     * 歌手详情.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'artist', name: '歌手详情', description: '', appId: '3aed503b9a', groupId: 1)]
    public function artist(): ResponseInterface
    {
        $id = $this->request->input('id', '');
        if (empty($id)) {
            return $this->response->error('请填写ID');
        }
        $res = $this->musicApi->artist($id);
        return $this->response->success('请求成功', [$res]);
    }

    /**
     * 歌单详情.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'topCategory', name: '榜单分类', description: '', appId: '3aed503b9a', groupId: 1)]
    public function topCategory(): ResponseInterface
    {
        $res = $this->musicApi->topCategory();
        return $this->response->success('请求成功', $res);
    }

    /**
     * 榜单歌曲详情.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'top', name: '榜单歌曲详情', description: '', appId: '3aed503b9a', groupId: 1)]
    public function top(): ResponseInterface
    {
        $id = $this->request->input('id', '');
        $page = $this->request->input('page', 1);
        $size = $this->request->input('size', 20);
        if (empty($id)) {
            return $this->response->error('请填写ID');
        }
        $res = $this->musicApi->top($id, $page, $size);
        return $this->response->success('请求成功', $res);
    }

    /**
     * 设置Cookie.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'setCookie', name: '设置Cookie', description: '', appId: '3aed503b9a', groupId: 1)]
    public function setCookie(): ResponseInterface
    {
        $cookie = $this->request->input('cookie', '');
        if (empty($cookie)) {
            return $this->response->error('请从qq音乐中获取Cookie');
        }
        $res = $this->musicApi->setCookie($cookie);
        return $this->response->success('请求成功', $res);
    }

    /**
     * 设置Cookie.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[MApi(accessName: 'getCookie', name: '获取Cookie', description: '', appId: '3aed503b9a', groupId: 1)]
    public function getCookie(): ResponseInterface
    {
        $cookie = $this->musicApi->getCookie();
        return $this->response->success('请求成功', ['cookie'=>$cookie]);
    }
}